<template>
  <div class="mobile_login">
    <div class="main">
      <div class="permission">
        <div class="permission-form">
          <div class="center_box">
            <div>
              <div>
                <img :src="logoSrc" alt="" style="width: 280px;">
              </div>
              <div class="title gl_my_10">跆拳道大满贯数字化管理平台</div>
            </div>
          </div>
          <forget-password ref="refForget">
            <template v-slot:FORGETWORD>
              <el-form :model="repasswordForm" @keydown.enter.prevent="return false;">
                <el-form-item prop="repassword" :rules="rules.repassword">
                  <el-input ref="repassword" v-model="repasswordForm.repassword" :placeholder="$t('rules.confirm')" type="password" name="repassword" tabindex="3" />
                </el-form-item>
              </el-form>
            </template>
            <template v-slot:FORGET>
              <el-button type="primary" class="gl_button_long" :loading="loading" @click.native.prevent="submitForget">{{ $t('forget.title') }}</el-button>
            </template>
          </forget-password>
          <el-row>
            <el-col align="center">
              <span style="font-size: 14px;color: #606266;">已知密码？</span>
              <el-button type="text" @click="routerRedirect('login')">
                <span>立即登录</span>
              </el-button>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>
    <div class="archival_copyright">
      <div class="archival">版权所有 © {{ copyright }}</div>
      <div class="copyright">
        <a :href="archivalUrl" class="gl_mr_10">苏ICP备{{ archival }}号</a>
        <a :href="archivalUrl">
          <img :src="archivalImg" alt="">
          苏公网安备 {{ archival }}号
        </a>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import { api_POST_UsersRegisterResetPassword } from '@/extend/api/platform/register';
import ForgetPassword from '@/plugin/ForgetPassword/mobileForgetPassword';
import archivalImg from '@/assets/images/beian.png';
import { RegisterDto_Rules } from '@/lib/validate/register/index.js';
export default {
  components: {
    ForgetPassword
  },
  data() {
    return {
      loading: false,
      bgSrc: require('@/assets/images/login_bg.png'),
      imgSrc: require('@/assets/images/login_bg1.png'),
      logoSrc: require('@/assets/images/logo/logo_word.png'),
      archivalImg: archivalImg,
      repasswordForm: { repassword: '' },
      rules: {
        repassword: new RegisterDto_Rules()['password']
      }
    };
  },
  computed: {
    ...mapState({
      archival: state => state.app.archival,
      archivalUrl: state => state.app.archivalUrl,
      copyright: state => state.app.copyright,
      copyrightUrl: state => state.app.copyrightUrl
    })
  },
  mounted() {},
  methods: {
    showrePwd() {
      if (this.repasswordType === 'repassword') {
        this.repasswordType = '';
      } else {
        this.repasswordType = 'repassword';
      }
    },
    ...mapActions({ setMarkTelephone: 'app/setMarkTelephone' }),
    // 找回密码按钮
    submitForget() {
      if (
        this.$refs.refForget['forgetDto'].password !==
        this.repasswordForm.repassword
      ) {
        this.$message.error('两次输入密码不一致！');
        return false;
      }
      this.$refs.refForget.$refs['forgetForm'].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$refs.refForget['forgetDto'].isNew = false; // isNew - true: 注册新用户；false: 已有用户（找回密码）
          api_POST_UsersRegisterResetPassword(this.$refs.refForget['forgetDto'])
            .then(res => {
              this.loading = false;
              if (res.result === 'Success') {
                this.$message({ message: '操作成功！', type: 'success' });
                this.setMarkTelephone(
                  this.$refs.refForget['forgetDto'].phoneNumber
                );
                this.$router.push({
                  name: 'login',
                  query: {
                    ReturnUrl: this.$route.query['ReturnUrl']
                  },
                  params: this.$route.params
                });
              } else if (res.result === 'AlreadyExists') {
                this.$message({ type: 'warning', message: '用户已经存在' });
              } else if (res.result === 'InvalidVerificationCode') {
                this.$message({ type: 'warning', message: '无效的验证码' });
              }
            })
            .catch(error => {
              this.loading = false;
              this.$message({ type: 'warning', message: error.error.message });
            });
        } else {
          return false;
        }
      });
    },
    routerRedirect(path) {
      // 返回登录
      this.$router.push({
        name: path,
        query: this.$route.query,
        params: this.$route.params
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.right-menu-item {
  float: right;
  padding: 20px 40px;
  height: 100%;
  font-size: 26px;
  color: #fff;
  vertical-align: text-bottom;

  &.hover-effect {
    cursor: pointer;
    transition: background 0.3s;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }
}
.main {
  min-height: calc(100vh - 54px);
}
.center_box {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin-bottom: 50px;
}
.archival_copyright {
  margin-bottom: 6px;
  width: 100%;
  height: 50px;
  padding-top: 10px;
  line-height: 20px;
  font-size: 12px;
  text-align: center;
  color: #bfbfbf;
}
.archival_copyright a {
  transition: all 0.3s ease;
}
.archival_copyright a:hover {
  text-decoration: underline;
}
.copyright img {
  vertical-align: middle;
  margin-right: 2px;
  width: 18px;
}
</style>
